﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rabbit.Model;
using System.Diagnostics;

namespace Rabbit.BusinessLogicLayer
{
    public class MetionedMeStatusBL : AutoUpdateMessageClassBase<Status>
    {
        public MetionedMeStatusBL(double UpdateInterval)
            : base(UpdateInterval)
        {

        }

        protected override void FetchNewMessage()
        {
            Debug.Print("");
            Debug.Print("Last Update Time:" + LastUpdateTime);
            List<Status> newStatusList = GetMetionedMeStatuses(LastMaxID).StatusList;
            newStatusList.ForEach(s => s.IsNew = true);
            if (newStatusList.Count > 0)
            {
#if DEBUG
                newStatusList.ForEach(s =>
                {
                    Debug.Print("status id:" + s.Id + " create at:" + s.CreatedAt + " text:" + s.Text);
                });
#endif
                base.LastMaxID = newStatusList.Max(s => s.Id);
            }

            //get the forwarded status picture url
            StatusesBL.GetForwardedStatusPicture(newStatusList);

            //batch get statuses' transmit and comment count, this includes:
            //MainMessageList which is passed from the UI, and the new status list
            List<Status> tempStatusList = new List<Status>();
            tempStatusList.AddRange(newStatusList);
            if (this.MainMessageList != null)
            {
                MainMessageList.ToList().ForEach(s => tempStatusList.Add(s));
            }
            StatusesBL.GetStatusCommentAndTransmitCount(tempStatusList);
            RaiseNewMessageReceivedEvent(newStatusList);
        }

        protected override void FetchMessage()
        {
            List<Status> StatusList = GetMetionedMeStatuses(null).StatusList;
            LastMaxID = StatusList.Max(s => s.Id);

            StatusesBL.GetForwardedStatusPicture(StatusList);
            StatusesBL.GetStatusCommentAndTransmitCount(StatusList);
            CurrentPageCursorID = -1;

            RaiseMessageFetchedEvent(StatusList);
        }
        internal Statuses GetMetionedMeStatuses(double? sinceID, long cursorID = -1)
        {
            string parameters = "cursor=" + cursorID;
            if (sinceID == null)
            {
                parameters += "&count=" + Utils.Configuration.PageSize;
            }
            else
            {
                parameters += "&since_id=" + sinceID.Value;
            }

            string statusString = RequestHelper.SendRequest("/statuses/mentions_timeline.json", parameters, "Get");
            Statuses tempStatuses = WrappedJsonHelper.Deserialize<Statuses>(statusString);
            return tempStatuses;
        }

        public override List<Status> GetNextPageMessage()
        {
            Statuses tempStatuses = GetMetionedMeStatuses(null, NextPageCursorID);

            //push previouse page cursor id to the queue
            CursorQueue.Push(CurrentPageCursorID.Value);
            CurrentPageCursorID = NextPageCursorID;

            //set next page cursor id to the returned value
            NextPageCursorID = tempStatuses.CursorID;
            return tempStatuses.StatusList;
        }

        public override List<Status> GetPreviousePageMessage()
        {
            if (CursorQueue.Count == 0)
            {
                return null;
            }

            CurrentPageCursorID = CursorQueue.Pop();

            Statuses tempStatuses = GetMetionedMeStatuses(null, CurrentPageCursorID.Value);
            NextPageCursorID = tempStatuses.CursorID;
            return tempStatuses.StatusList;
        }

        public override List<Status> GetFirstPageMessage()
        {
            CursorQueue.Clear();

            Statuses tempStatuses = GetMetionedMeStatuses(null, -1);
            NextPageCursorID = tempStatuses.CursorID;
            CurrentPageCursorID = -1;
            return tempStatuses.StatusList;
        }
    }
}
